<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------
namespace app\admin\controller;
use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use \think\Session;
use app\admin\controller\Permissions;
use app\admin\model\Nav as navModel;
class Nav  extends Permissions
{
    public function index()
    {
        $model = new navModel();
        $post = $this->request->param();
        //查询条件，模糊查询
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['navname'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if (isset($post['article_cate_id']) and $post['article_cate_id'] > 0) {
            $where['article_cate_id'] = $post['article_cate_id'];
        }

        if (isset($post['admin_id']) and $post['admin_id'] > 0) {
            $where['admin_id'] = $post['admin_id'];
        }
        
        if (isset($post['status']) and ($post['status'] == 1 or $post['status'] === '0')) {
            $where['status'] = $post['status'];
        }

        if (isset($post['is_top']) and ($post['is_top'] == 1 or $post['is_top'] === '0')) {
            $where['is_top'] = $post['is_top'];
        }
        //显示条数
        $nav= empty($where) ? $model->order('modadmin_id desc')->paginate(50) : $model->where($where)->order('modadmin_id desc')->paginate(20,false,['query'=>$this->request->param()]);
        //$nav = $nav->toArray();
        //添加最后修改人的name
        foreach ($nav as $key => $value) {
            $nav[$key]['modadmin_id'] = Db::name('admin')->where('id',$value['modadmin_id'])->value('nickname');
        }
        //执行menuslist
        $navs = $model->menulist($nav);
        $this->assign('nav',$navs);
        return $this->fetch();
    }

    public function publish()
    {
        //获取菜单id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new navModel();
        //是正常添加操作
        if($id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['navname', 'require', '菜单名称不能为空'],
                    ['pid', 'require', '请选择上级菜单'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证菜单是否存在
                $menu = $model->where('id',$id)->find();
                if(empty($menu)) {
                    return $this->error('id不正确');
                }
                //如果关闭默认展开，给默认值0
                if(empty($post['is_open'])) {
                    $post['is_open'] = 0;
                }
                if(false == $model->allowField(true)->save($post,['id'=>$id])) {
                    return $this->error('修改失败');
                } else {
                    addlog($model->id);//写入日志
                    return $this->success('修改菜单信息成功','admin/nav/index');
                }
            } else {
                //非提交操作
                $menu = $model->where('id',$id)->find();
                $menus = $model->select();
                $menus_all = $model->menulist($menus);
                $this->assign('menus',$menus_all);
                if(!empty($menu)) {
                    $this->assign('nav',$menu);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');

                }
            }
        }
        else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['navname', 'require', '菜单名称不能为空'],
                    ['pid', 'require', '请选择上级菜单'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加菜单失败');
                } else {
                    return $this->success('添加菜单成功','admin/nav/index');
                }
            } else {
                //非提交操作
                $pid = $this->request->has('pid') ? $this->request->param('pid', null, 'intval') : null;
                if(!empty($pid)) {
                    $this->assign('pid',$pid);
                }
                $menu = $model->select();
                $menus = $model->menulist($menu);
                $this->assign('menus',$menus);
                return $this->fetch();
            }
        }

    }



    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('nav')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                addlog($id);//写入日志
                return $this->success('删除成功','admin/nav/index');
            }
        }
    }


    public function is_top()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            $is_top = $post['is_top'];
            if(false == Db::name('nav')->where('id',$post['id'])->update(['is_top'=>$post['is_top']])) {
                return $this->error('设置失败');
            } else {
                addlog($post['id']);//写入日志
                return $this->success('设置成功','admin/nav/index');
            }
        }
    }


    public function status()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            $status = $post['status'];
            if(false == Db::name('nav')->where('id',$post['id'])->update(['status'=>$post['status']])) {
                return $this->error('设置失败');
            } else {
                addlog($post['id']);//写入日志
                return $this->success('设置成功','admin/nav/index');
            }
        }
    }
}
